package com.mtao.controller.admin;

import com.mtao.*;
import com.mtao.dto.BannerDto;
import com.mtao.dto.IdDto;
import com.mtao.utils.JwtUtils;
import com.mtao.utils.StringUtil;
import com.mtao.utils.ThreadLocalUtil;
import com.mtao.entity.Result;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/banner")
public class BannerController {

    @Resource
    private BannerService BannerService;

    @Resource
    private JwtUtils jwtUtils;

    /**
     * 获取轮播图列表
     * @return 返回角色列表
     */
    @PostMapping("/list")
    public com.mtao.entity.Result<PageBean<com.mtao.entity.Banner>> getRoleList(@RequestBody @Validated(BannerDto.Add.class) BannerDto bannerDto){
        PageBean<com.mtao.entity.Banner> list= BannerService.bannerList(bannerDto);
        return com.mtao.entity.Result.success(list);
    }
    /**
     * 添加轮播图
     * @param banner 轮播图
     * @return 返回添加结果
     */
    @PostMapping("/add")
    public com.mtao.entity.Result addBanner(@RequestBody @Validated(com.mtao.entity.Banner.Add.class) com.mtao.entity.Banner banner){
        Map<String,Object> map= (Map<String,Object>) ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        banner.setUserId(userId);
        BannerService.addBanner(banner);
        return com.mtao.entity.Result.success();
    }


    /**
     * 删除轮播图
     * @param idDto 横幅对象
     * @return 删除结果
     */
    @PostMapping("/delete")
    public com.mtao.entity.Result deleteBanner(@RequestBody IdDto idDto){
        int[] ints = StringUtil.StringToNumberArray(idDto.getId());
        System.out.println(ints);
        for (int anInt : ints) {
            BannerService.deleteBanner(anInt);
        }
        return Result.success();
    }


}
